package com.zxj.geekstudy.dao.impl;

import com.zxj.geekstudy.dao.StudentDao;
import com.zxj.geekstudy.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StudentDaoImpl implements StudentDao {

    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public List<Student> list() {
        String sql = "select name, score from student ";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(
                Student.class));
    }

    @Override
    public void create(Student student) {
        StringBuffer sb = new StringBuffer();
        sb.append("insert into student(name, score) values (?,?)");
        jdbcTemplate.update(sb.toString(), student.getName(), new BigDecimal(10));
    }
}
